<?php
namespace Admin\Controller;
use Think\Controller;

class AuthController extends CommonController
{
    public function _initialize()
    {
        // 数组存放不需添加进权限控制的方法
        $params = array('statuschange' ,'bacthdo');
        parent::_initialize($params);
    }

    // 角色列表
    public function index()
    {
        $roles = M("role")->where("role_status = 1")->field("role_id,name")->select();
        foreach($roles as $key => $val){
            $roles[$key]["users"] = M("role_manager")->where("role_id = ".$val['role_id'])->count();
        }
        $this->assign("roles" ,$roles);
        $this->display();
    }

    // 管理员列表
    public function administrators()
    {
        $role = D("RoleView");
        $where = array();
        if(isset($_REQUEST['mName']))
        {
            $mName = trim($_REQUEST['mName']);
            $roleId = trim($_REQUEST['roleId']);
            $status = trim($_REQUEST['sta']);

            if($mName != ''){
                $where['manager.name'] = array("like" ,"%$mName%");
            }
            if($status != ''){
                $where['manager.m_status'] = array("eq" ,$status);
            }
            if($roleId != ''){
                $where['role_manager.role_id'] = array("eq" ,$roleId);
            }
        }
        $count = $role->where($where)->count();
        $page = set_page($count, C('PAGE_NUM'));
        $admins = $role->where($where)->limit($page->firstRow.','.$page->listRows)->select();
        $show = $page->show();
        $this->assign('page',$show);
        $this->assign('admins', $admins);
        $this->roleList = M("role")->where("role_status = 1")->select();
        $this->display();
    }

    // 角色添加
    public function add_role()
    {
        if(IS_POST)
        {
            $role_name = trim(I("post.role_name"));
            if($role_name == ''){
                $this->error("角色名必填" ,$_SERVER['HTTP_REFERER'] ,1);
            }
            $rs = M("role")->add(array('name' => $role_name));
            if($rs !== false){
                $this->success("添加成功" ,U("Auth/set_auth", array('id' => M("role")->getLastInsID())) ,1);
            }else{
                $this->error("添加失败" ,$_SERVER['HTTP_REFERER'] ,1);
            }
        }else{
            $this->display();
        }
    }

    // 角色名称编辑
    public function changeRoleName()
    {
        if(IS_AJAX)
        {
            $role_name = trim(I("post.role_name"));
            $role_id = to_int(I("post.role_id"));
            if($role_name == '' || $role_id == 0){
                $this->ajaxReturn(array("status" => 'error' ,'msg' => '参数错误'));
            }
            $rs = M("role")->where("role_id = $role_id")->save(array('name' => $role_name));
            if($rs !== false){
                $this->ajaxReturn(array("status" => 'success'));
            }else{
                $this->ajaxReturn(array("status" => 'error' ,'msg' => '修改失败了'));
            }
        }else{
            exit("error");
        }
    }

    // 角色删除
    public function del_role()
    {
        if(IS_AJAX)
        {
            $roleid = to_int(I("post.roleid"));
            if($roleid == 0){
                $this->ajaxReturn(array("status" => 'error' ,'msg' => '参数错误'));
            }
            $hasManager = M("role_manager")->where("role_id = $roleid")->find();
            if($hasManager){
                $this->ajaxReturn(array("status" => 'error' ,'msg' => '有用户不可删除'));
            }
            $rs = M("role")->where("role_id = $roleid")->save(array('role_status' => 0));
            if($rs !== false){
                $this->ajaxReturn(array("status" => 'success'));
            }else{
                $this->ajaxReturn(array("status" => 'error' ,'msg' => '删除失败'));
            }
        }else{
            exit("error");
        }
    }

    // 权限分配
    public function set_auth()
    {
        if(IS_POST)
        {
            $role_id = to_int(I("post.role_id"));
            $urls = I("post.urls");
            if(!isset($_POST['urls'])){
                $urls = array();
            }
            if(!is_array($urls) ||$role_id == 0){
                $this->error("请求错误" ,$_SERVER['HTTP_REFERER'] ,1);
            }
            if(empty($urls)){
                M("permission")->where("role_id = $role_id")->delete();
                $this->success("分配成功" ,$_SERVER['HTTP_REFERER'] ,1);
            }else{
                foreach($urls as $url){
                    if(!nodeUrlRule($url)){
                        $this->error("节点选择有误" ,$_SERVER['HTTP_REFERER'] ,1);
                    }
                }
                M("permission")->where("role_id = $role_id")->delete();
                foreach($urls as $url){
                    M("permission")->add(array('url' => $url ,'role_id' => $role_id));
                }
                $this->success("分配成功" ,$_SERVER['HTTP_REFERER'] ,1);
            }
        }else{
            $role_id = intval(I("get.id"));
            $role = M("role")->where("role_id = $role_id")->find();
            if(!$role){
                $this->error("请求失败" ,$_SERVER['HTTP_REFERER'] ,1);
            }
            $urls = array();
            $perurls = M("permission")->where("role_id = $role_id")->field("url")->select();
            if($perurls){
                foreach($perurls as $url){
                    $urls[] = $url['url'];
                }
            }
            $this->assign("role" ,$role);
            $this->assign("urls" ,$urls);
            $this->display();
        }
    }

    // 添加管理员
    public function add_administrators()
    {
        if(IS_POST)
        {
            $name = trim(I("post.name"));
            $mobile = trim(I("post.mobile"));
            $email = trim(I("post.email"));
            $role_id = to_int(I("post.role_id"));
            $pwd = trim(I("post.pwd"));
            if($name == '' || $mobile == '' || $email == '' || $role_id == 0 || $pwd ==''){
                $this->error("信息填写不完整" ,$_SERVER['HTTP_REFERER'] ,1);
            }
            $insertData['name'] = $name;
            $insertData['mobile'] = $mobile;
            $insertData['email'] = $email;
            $insertData['salt'] = substr(md5(time() . $name) ,-5);
            $insertData['password'] = md5(md5($pwd) .$insertData['salt']);
            $rs = M("manager")->add($insertData);
            if($rs !== false){
                M("role_manager")->add(array('role_id' => $role_id ,'manager_id' => M("manager")->getLastInsID()));
                $this->success("添加成功" ,U("Auth/administrators") ,1);
            }else{
                $this->error("添加失败" ,$_SERVER['HTTP_REFERER'] ,1);
            }
        }else{
            $this->roleList = M("role")->where("role_status = 1")->select();
            $this->display();
        }
    }

    // 编辑管理员
    public function edit_admin()
    {
        if(IS_POST)
        {
            $mobile = trim(I("post.mobile"));
            $email = trim(I("post.email"));
            $m_status = to_int(I("post.m_status"));
            $role_id = to_int(I("post.role_id"));
            $manager_id = to_int(I("post.manager_id"));
            $pwd = trim(I("post.pwd"));
            if($mobile == '' || $email == '' || $role_id == 0){
                $this->error("信息填写不完整" ,$_SERVER['HTTP_REFERER'] ,1);
            }
            $manager = M("manager")->where("id = $manager_id")->find();
            if(!$manager){
                $this->error("非法操作!" ,$_SERVER['HTTP_REFERER'] ,1);
            }
            $saveData['mobile'] = $mobile;
            $saveData['email'] = $email;
            $saveData['m_status'] = $m_status;
            if($pwd !=''){
                $saveData['password'] = md5(md5($pwd) . $manager['salt']);
            }
            // 超管改掉状态，不可关闭的账号
            if($manager_id == 1) $saveData['m_status'] = 1;
            $rs = M("manager")->where("id = $manager_id")->save($saveData);
            if($rs !== false){
                M("role_manager")->where("manager_id = $manager_id")->save(array('role_id' => $role_id));
                $this->success("修改成功" ,U("Auth/administrators") ,1);
            }else{
                $this->error("修改失败" ,$_SERVER['HTTP_REFERER'] ,1);
            }
        }else{
            $manager_id = to_int(I("get.id"));
            $manager = D("RoleView")->where("manager.id = $manager_id")->find();
            if(!$manager){
                $this->error("请求失败" ,$_SERVER['HTTP_REFERER'] ,1);
            }
            $this->manager = $manager;
            $this->roleList = M("role")->where("role_status = 1")->select();
            $this->display();
        }
    }

    // 编辑管理员状态
    public function statusChange()
    {
        if(IS_AJAX)
        {
            $manager_id = to_int(I("post.id"));
            if($manager_id == 0){
                $this->ajaxReturn(array("status" => 'error' ,'msg' => '参数错误'));
            }
            if($manager_id == 1){
                $this->ajaxReturn(array("status" => 'error' ,'msg' => '超管不可修改'));
            }
            $manager = M("manager")->where("id = $manager_id")->find();
            if(!$manager){
                $this->ajaxReturn(array("status" => 'error' ,'msg' => '无此管理员账号'));
            }
            if($manager["m_status"] == 1){
                $status = 0;
            }else{
                $status = 1;
            }
            $rs = M("manager")->where("id = $manager_id")->save(array("m_status" => $status));
            if($rs !== false){
                $this->ajaxReturn(array("status" => 'success'));
            }else{
                $this->ajaxReturn(array("status" => 'error' ,'msg' => '修改失败了'));
            }
        }else{
            exit("error");
        }
    }

    // 批量编辑管理员状态
    public function bacthDo()
    {
        if(IS_POST)
        {
            $do_type = I("post.type");
            $manager_ids = I("post.id");
            if(!is_array($manager_ids) || empty($manager_ids)){
                $this->error("参数有误" ,U('Auth/administrators') ,1);
            }
            foreach($manager_ids as $id){
                if(!_int($id)){
                    $this->error("参数有误" ,U('Auth/administrators') ,1);
                }
                if($id != 1){
                    $ids[] = $id;
                }
            }
            $where["id"] = array("in" ,implode("," ,$ids));
            // 批量开启
            if($do_type == 'open'){
                $rs = M("manager")->where($where)->save(array("m_status" => 1));
                if($rs !== false){
                    $this->success("操作成功" ,U('Auth/administrators') ,1);
                }else{
                    $this->error("操作失败" ,U('Auth/administrators') ,1);
                }
            }
            // 批量关闭
            if($do_type == 'close'){
                $rs = M("manager")->where($where)->save(array("m_status" => 0));
                if($rs !== false){
                    $this->success("操作成功" ,U('Auth/administrators') ,1);
                }else{
                    $this->error("操作失败" ,U('Auth/administrators') ,1);
                }
            }
        }else{
            exit("error");
        }
    }
}